from django.shortcuts import render

# Create your views here.


from django.shortcuts import render, get_object_or_404

from .models import Post

from django.db.models import Q


def index (request):
    """
    :param request:
    :return:
    """
    # post_list = Post.objects.all()
    post_list = Post.objects.order_by('publish')

    return render(request, 'index.html', locals())



def search(request):
    key = request.GET.get('key')
    error_msg = ''

    if not key:
        error_msg = "空内容;请输入关键词"
        return render(request, 'index.html', locals())
    else:
        post_list = Post.objects.filter(
            Q(title__icontains=key)|
            Q(body__icontains=key)
            )
        if post_list:
            return render(request, 'index.html',locals())
        else:
            error_msg = "没有搜索到内容"
            return render(request, 'index.html', locals())